<?php
/**
 * Created by PhpStorm.
 * Author: wenwenbin
 * Date: 2018/2/24
 * Time: 17:21
 */
namespace App\Http\Admin\Personnel\Models;

use App\Eloquent\Oa\TalentPool;
use App\Eloquent\Oa\WorkHistory;
use Framework\BaseClass\Http\Admin\Model;

class Resume extends Model
{
    public function getResignPersonnelList($company_id, $paging)
    {
        $workHistoryList = WorkHistory::where([['oa_company_id', $company_id], ['end_time', '>', 0]])
            ->with(['contactsInfo' => function ($q) {$q->withTrashed();}, 'departmentInfo', 'talentPoolInfo'])
            ->orderBy('start_time', 'desc')
            ->offset(($paging['page'] - 1) * $paging['page_size'])
            ->take($paging['page_size'])
            ->get();
        foreach ($workHistoryList as $workHistory) {
            $newestHistory = WorkHistory::where('oa_contacts_id', $workHistory->oa_contacts_id)->orderBy('start_time', 'desc')->first();
            if ($workHistory->id != $newestHistory->id) {
                $workHistory->status = '已加入公司';
            } elseif (is_null($workHistory->talentPoolInfo)) {
                $workHistory->status = '可加入人才库';
            } else {
                $workHistory->status = '已加入人才库';
            }
            $workHistory->contacts_name = $workHistory->contactsInfo ? $workHistory->contactsInfo->name : '';
            $workHistory->mobile = $workHistory->contactsInfo ? $workHistory->contactsInfo->mobile : '';
            $workHistory->department_name = $workHistory->departmentInfo ? $workHistory->departmentInfo->name : '';
        }
        return $workHistoryList;
    }

    public function saveTalentPool($id)
    {
        $workHistory = WorkHistory::find($id);
        if (is_null($workHistory)) xThrow(ERR_OA_WORK_HISTORY_NOT_EXIST);
        $talentPool = new TalentPool();
        $talentPool->fill([
            'oa_work_history_id' => $workHistory->id,
            'oa_company_id'      => $workHistory->oa_company_id,
            'oa_department_id'   => $workHistory->oa_department_id,
            'oa_contacts_id'     => $workHistory->oa_contacts_id,
            'position'           => $workHistory->position,
            'resign_time'        => $workHistory->end_time,
        ]);
        return $talentPool->save();
    }
}